package h0704;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

//五键键盘
public class FiveKey_KeyBoard {
    //屏幕上的字
    List<Integer> res = new ArrayList<>();
    //剪切板
    List<Integer> clipBoard = new ArrayList<>();
    //当前选择的字母
    List<Integer> selected = new ArrayList<>();

    public static void main(String[] args) {
        //自己写的解法
//        Scanner sc = new Scanner(System.in);
//        String s = sc.nextLine();
//        String[] numbers = s.split(" ");
//        List<Integer> list = new ArrayList<>();
//        for (String number : numbers) {
//            list.add(Integer.parseInt(number));
//        }
//        System.out.println(list);
//        FiveKey_KeyBoard board = new FiveKey_KeyBoard();
//        System.out.println(board.getResult(list));

        //官方解法
        //读入数组
        Scanner sc = new Scanner(System.in);
        int[] commands = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        System.out.println(getResult(commands));
    }

    public int getResult(List<Integer> nums) {
        for (Integer num : nums) {
            pressKey(num);
        }
        return res.size();
    }

    public void pressKey(int num) {
        switch (num) {
            case 1://a
                //如果没有全选
                if (selected.isEmpty()){
                    res.add(1);
                }else {
                    //如果有全选则覆盖
                    res.clear();
                    res.add(1);
                }
                break;
            case 2://ctrl+c
                if (!selected.isEmpty()) {
                    //如果ctrl+a不为空，直接复制到剪切板
                    clipBoard.addAll(selected);
                }
                break;
            case 3://ctrl+x
                if (!selected.isEmpty()) {
                    //ctrl+a不为null，剪切直接清空屏幕
                    res.clear();
                    //无选中状态
                    selected.clear();
                }
                break;
            case 4://ctrl+v
                if (!clipBoard.isEmpty()) {
                    if (!selected.isEmpty()){
                        //如果全选不为空，并且剪切板不空。直接替换当前选择的。
                        res = new ArrayList<>(clipBoard);
                        selected.clear();
                    }else {
                        //如果全选不为空，并且剪切板未空。不变
                        //如果全选为空，并且剪切板不为空。后面增加
                        res.addAll(clipBoard);
                    }
                }
                break;
            case 5:
                selected = new ArrayList<>(res);
                break;
        }
    }

    public static int getResult(int[] commands) {
        //定义两个List
        ArrayList<String> screen = new ArrayList<>();
        ArrayList<String> clip = new ArrayList<>();
        boolean isSelect = false;

        for (int command : commands) {
            switch (command) {
                case 1: // a
                    if (isSelect) screen.clear();
                    screen.add("a");
                    isSelect = false;
                    break;
                case 2: // ctrl-c
                    if (isSelect) {
                        clip.clear();
                        clip.addAll(screen);
                    }
                    break;
                case 3: // ctrl-x
                    if (isSelect) {
                        clip.clear();
                        clip.addAll(screen);
                        screen.clear();
                        isSelect = false;
                    }
                    break;
                case 4: // ctrl-v
                    if (isSelect) screen.clear();
                    screen.addAll(clip);
                    isSelect = false;
                    break;
                case 5: // ctrl-a
                    if (screen.size() != 0) isSelect = true;
                    break;
            }
        }

        return screen.size();
    }
}
